home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_052 / tek4010 / script.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  10KB  |  371 lines

  1. /*************************************************************
  2.  * vt100 terminal emulator - Script file support
  3.  *
  4.  *           860823 DBW - Integrated and rewrote lots of code
  5.  *           860815 Steve Drew: Initial version written of SCRIPT.C
  6.  *      v2.0 860809 DBW - Major rewrite
  7.  *      v1.1 860720 DBW - Switches, 80 cols, colors, bug fixes
  8.  *      v1.0 860712 DBW - First version released
  9.  *
  10.  *************************************************************/
  11.  
  12. #define MODULE_SCRIPT 1
  13.  
  14. #include "vt100.h"
  15.  
  16. extern  char *fgets(),*malloc();
  17. extern  long ftell();
  18. extern  int XMODEM_Send_File(), XMODEM_Read_File(), doksend(), dokreceive();
  19. struct COMMAND {
  20.                  int (*func)();
  21.                  char *cname;
  22. };
  23.  
  24. struct LABEL  {
  25.                  struct LABEL *next;
  26.                  char *name;
  27.                  long pos;
  28. };
  29.  
  30.  
  31.  
  32. /****************  globals  needed  ******************/
  33.  
  34. char            on_string[20];       /* string to match on for on cmd    */
  35. char            wait_string[20];     /* string to match of for wait cmd  */
  36. char            golabel[20];         /* label we are looking for in goto */
  37. char            on_cmd[255];         /* command to execute when on matchs*/
  38. int             onsize;              /* size of on_string                */
  39. int             waitsize;            /* size of wait_string              */
  40. int             onpos;               /* position in on string for search */
  41. int             waitpos;             /* pos in wait_string for search    */
  42. int             on_match;            /* flag set while doing on_cmd      */
  43. FILE            *sf;                 /* file pointer for script file     */
  44. struct LABEL    *lbase = NULL;       /* will point to first label        */
  45. struct LABEL    *labels;             /* current label pointer            */
  46.  
  47. int  cmd_send(), cmd_wait(), cmd_on(), cmd_goto(), cmd_delay(), cmd_done(),
  48.      cmd_ks(), cmd_kg(), cmd_kr(), cmd_xs(), cmd_xr(), cmd_cap(), cmd_as(),
  49.      cmd_null(), cmd_kb();
  50.  
  51. char *next_wrd(), *tostring();
  52.  
  53.  
  54. /********************** command table **********************************/
  55.  
  56. static struct COMMAND commands[]= {
  57.                  cmd_send,   "SEND",         /* send string to host      */
  58.                  cmd_wait,   "WAIT",         /* wait for a string from host */
  59.                  cmd_on,     "ON",           /* on a 'string' do a cmd   */
  60.                  cmd_goto,   "GOTO",         /* goto label               */
  61.                  cmd_delay,  "DELAY",        /* delay amount of seconds  */
  62.                  cmd_done,   "EXIT",         /* exit script file         */
  63.                  cmd_ks,     "KS",           /* kermit send file         */
  64.                  cmd_kr,     "KR",           /* kermit receive file      */
  65.                  cmd_kg,     "KG",           /* kermit get file          */
  66.                  cmd_kb,     "KB",           /* kermit bye (for server)  */
  67.                  cmd_xs,     "XS",           /* xmodem send file         */
  68.                  cmd_xr,     "XR",           /* xmodem receive file      */
  69.                  cmd_cap,    "CAPTURE",      /* ascii capture on/off     */
  70.                  cmd_as,     "ASCII_SEND",   /* ascii send               */
  71.                  cmd_null,   NULL
  72. };
  73.  
  74. /********************************************************************/
  75. /* checks char to see if match with on string or wait_string        */
  76. /* if on string match oncmd gets executed imediately,               */
  77. /* if wait_string match script_wait is set.                         */
  78. /********************************************************************/
  79.  
  80. chk_script(c)
  81. char c;
  82. {
  83.     if (on_string[0] != '\0') {
  84.         if (on_string[onpos] == c) {
  85.             onpos++;
  86.             if (onpos == onsize) {
  87.                 on_match = TRUE;
  88.                 do_script_cmd(ONCOMMAND); 
  89.                 on_match = FALSE;
  90.                 return(0);
  91.             }
  92.         }
  93.         else onpos = 0;
  94.     }
  95.  
  96.     if (wait_string[0] != '\0') {
  97.        if (wait_string[waitpos] != c) {
  98.             waitpos = 0;
  99.             return(0);
  100.         }
  101.         waitpos++;
  102.         if (waitpos != waitsize) return(0);
  103.         wait_string[0] = '\0';
  104.         script_wait = FALSE;
  105.     }
  106. }
  107.  
  108. script_start(file)
  109. char *file;
  110. {
  111.     if ((sf = fopen(file, "r")) == NULL) {
  112.         emits("Can't open script file\n");
  113.         return(0);
  114.     }
  115.     script_on = TRUE;
  116.     script_wait = FALSE;
  117.     wait_string[0] = '\0';
  118.     on_string[0] = '\0';
  119.     on_match = FALSE;
  120.     lbase = NULL;
  121. }
  122.  
  123. /* return pointer to next word. set l to size of the word */
  124.  
  125. char *next_wrd(s,l)
  126. char *s;
  127. int *l;
  128. {
  129.     char *p;
  130.     while(*s && (*s == ' ' || *s == 9)) s++;
  131.     p = s;
  132.     while(*s && (*s != ' ' && *s != 9)) s++;
  133.     *l = s-p;
  134.     return(p);
  135. }
  136.  
  137. exe_cmd(p,l)
  138. char *p;
  139. int l;
  140. {
  141.     int i;
  142.     
  143.     for (i=0; commands[i].func != cmd_null; ++i) 
  144.         if (strncmp(p, commands[i].cname, l) == 0) {
  145.             (*commands[i].func)(next_wrd(p+l, &l));
  146.             return(TRUE);
  147.         }
  148.         emits ("\nScript - unknown command: ");
  149.         emits (p);
  150.         emits ("\n"); 
  151. }
  152.  
  153. struct LABEL *find_label(lname)
  154. char *lname;
  155. {
  156.     struct LABEL *label;
  157.   
  158.     label = lbase;
  159.     while(label != NULL) {
  160.        if (strcmp(label->name, lname) == 0) return (label);
  161.        label = label->next;
  162.     }
  163.     return(NULL);
  164. }
  165.  
  166.  
  167. do_script_cmd(stat)
  168. int stat;
  169. {
  170.     int len,l;
  171.     char line[256];
  172.     char *p;
  173.  
  174.     if (stat == ONCOMMAND) {    /* if ON command is matched and we were     */
  175.         strcpy(line,on_cmd);    /* doing a DELAY then abort the delay timer,*/
  176.         p = next_wrd(line,&l);  /* except if on_cmd was just a SEND.        */
  177.         if (*p != 'S' && script_wait == WAIT_TIMER)  {
  178.             AbortIO((char *) &Script_Timer);
  179.             Wait (1L << Script_Timer_Port->mp_SigBit);
  180.             script_wait = FALSE; /* script will proceed after on command    */
  181.         }
  182.         exe_cmd(p,l);
  183.         return(0);
  184.     }
  185.  
  186.     script_wait = FALSE;
  187.     while(fgets(line,256,sf) != NULL) {
  188.        len = strlen(line);
  189.        line[--len] = '\0';
  190.        p = next_wrd(&line[0], &l);
  191.        if (*(p + l - 1) == ':') {               /* its a label */
  192.            *(p + l - 1) = '\0';
  193.            if (find_label(p) == NULL) {         /* it's a new label */
  194.                if (lbase == NULL)  {            /* it's the first label */
  195.                    labels = lbase = (struct LABEL *) malloc (sizeof (struct LABEL));
  196.                }
  197.                else {
  198.                    labels->next = 
  199.                            (struct LABEL *) malloc (sizeof (struct LABEL));
  200.                    labels = labels->next;
  201.                }
  202.                labels->pos  = ftell(sf);
  203.                labels->name = malloc(l);
  204.                labels->next = NULL;
  205.                strcpy(labels->name, p);
  206.                if (stat == GOTOLABEL && strcmp(p, golabel) == 0) 
  207.                       stat = NEXTCOMMAND;
  208.            }
  209.            if (l < len) p = next_wrd(p+l+1, &l);
  210.            else return(0);
  211.        }        /* end of it's a label */
  212.        if (stat == GOTOLABEL || *p == '#') continue;
  213.        if (*p) exe_cmd(p,l);
  214.        return(0);
  215.     }           /* end of while */
  216.     if (stat == GOTOLABEL) {
  217.           emits("\nScript - label not found: ");
  218.           emits(golabel);
  219.           emits("\n");
  220.     }         
  221.     exit_script();
  222. }
  223.  
  224. exit_script()
  225. {
  226.     if (script_wait == WAIT_TIMER)      /* timer not done yet */
  227.        AbortIO((char *) &Script_Timer); /* so abort it */
  228.     emits("\nScript - terminated\n");    
  229.     script_on = FALSE;
  230.     script_wait = TRUE;
  231.     fclose(sf);
  232. }
  233.  
  234. /* remove quotes terminate string & return pointer to start */
  235.  
  236. char *tostring(ptr)
  237. char *ptr;
  238. {
  239.     char *s;
  240.  
  241.     s = ptr;
  242.     if (*ptr == '"') {
  243.          while(*ptr++  && *ptr !='"');
  244.          if (*ptr == '"') {
  245.              *ptr = '\0';  
  246.              ptr = s+1;
  247.              while(*s++)  if (*s == '|') *s = '\r';
  248.              return(ptr);
  249.          }
  250.     }
  251.     if (*s == '^') {
  252.         *s = *(s+1)-64;
  253.         *(s+1) = '\0';
  254.         return(s);
  255.         }
  256.     *(s+1) = '\0';
  257.     return(s);
  258. }   
  259.         
  260. cmd_goto(lname)
  261. char *lname;
  262. {
  263.     struct LABEL *label;
  264.                             /* if on_cmd was a goto kill wait state */
  265.     if (on_match) { wait_string[0] = '\0'; script_wait = FALSE; }
  266.     if ((label = find_label(lname)) == NULL) {  /* is it forward */
  267.         strcpy(golabel,lname);
  268.         do_script_cmd(GOTOLABEL);
  269.     }
  270.     else {
  271.         fseek(sf,(long)(label->pos),0);
  272.     }
  273. }
  274.  
  275. cmd_send(str)
  276. char *str;
  277. {
  278.   sendstring(tostring(str));
  279. }
  280.  
  281.  
  282. cmd_wait(str)
  283. char *str;
  284. {
  285.         str = tostring(str);
  286.         *(str+20) = '\0';         /* 20 characters max */
  287.         strcpy(wait_string, str);
  288.         waitsize = strlen(str);
  289.         script_wait = WAIT_STRING;
  290. }
  291.  
  292. cmd_on(str)
  293. char *str;
  294. {
  295.    char *p;
  296.  
  297.    p = tostring(str);
  298.    strcpy(on_string, p);
  299.    onsize = strlen(p);
  300.    *(p+onsize+2+20) = '\0';        /* 20 characters max */
  301.    strcpy(on_cmd,p+onsize+2);
  302. }
  303.  
  304. cmd_delay(seconds)
  305. char *seconds;
  306. {
  307.     script_wait = WAIT_TIMER;
  308.     Script_Timer.tr_time.tv_secs = atoi(seconds);
  309.     Script_Timer.tr_time.tv_micro = 0;
  310.     SendIO((char *) &Script_Timer.tr_node);
  311. }
  312.  
  313. cmd_done()
  314. {
  315.     exit_script();
  316. }
  317.  
  318. cmd_ks(file)
  319. char *file;
  320. {
  321.    multi_xfer(file, doksend, 1);
  322. }
  323.  
  324. cmd_kr(file)
  325. char *file;
  326. {
  327.    multi_xfer(file, dokreceive, 0);
  328. }
  329.  
  330. cmd_kg(file)
  331. char *file;
  332. {
  333.    server = TRUE;
  334.    multi_xfer(file, dokreceive, 0);
  335. }
  336.  
  337. cmd_kb()
  338. {
  339.    saybye();
  340. }
  341.  
  342. cmd_xs(file)
  343. char *file;
  344. {
  345.    multi_xfer(file, XMODEM_Send_File, 1);
  346. }
  347.  
  348. cmd_xr(file)
  349. char *file;
  350. {
  351.    multi_xfer(file, XMODEM_Read_File, 1);
  352. }
  353.  
  354. cmd_cap(file)
  355. char *file;
  356. {
  357. do_capture(file);
  358. }
  359.  
  360. cmd_as(file)
  361. char *file;
  362. {
  363. do_send(file);
  364. }
  365.  
  366. cmd_null(file)
  367. char *file;
  368. {
  369. }
  370.  
  371.